<?php

namespace addons\TfFutures\common\models;

use addons\TfFutures\common\models\Symbol as SymbolXh;
use common\models\member\Member as Member2;

use Yii;
use yii\db\ActiveRecord;

/**
 * 用户应用信息表.
 */
class OrderReason extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_tf_futures_order_reason}}';
    }

    public function rules()
    {
        return [
            [['member_id'], 'required'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'                => 'ID',
            'member_id'         => '用户id',
            'symbol'            => '产品代码',
            'symbol_cn'         => '产品名称',
            'contract_size'     => '合约量',
            'rise_fall'         => '买卖类型',
            'volume'            => '手数',
            'deposit'           => '保证金',
            //'lever_id'          => '杠杆id',
            'limit'             => '限制条件',
            'limit_num'         => '触发次数',
            'limit_num_cal'     => '已触发',
            'limit_time'        => '限制条件到期/开始时间',
            'limit_price'       => '限制条件的价格',
            'open_price'        => '设定价格',
            'stop_profit_price' => '止盈金额',
            'stop_profit_point' => '止盈点数',
            'stop_loss_price'   => '止损金额',
            'stop_loss_point'   => '止损点数',
            'expiration'        => '到期时间',
            'state'             => '订单状态',
            'status'            => '状态',
            'create_at'         => '创建时间',
            'update_at'         => '修改时间',
            'service_fee'       => '手续费',
            'price'     		=> '设定价格',
        ];
    }

    /**
     * 验证价格
     * @param $product             产品 || 产品id
     * @param $productcmd          买卖类型
     * @param $rise_fall           买卖方向
     * @param $price               验证价格
     * @param $stop_profit_price   止盈价格
     * @param $stop_loss_price     止损价格
     * @return mixed
     * @throws \Exception
     *                             **/
    /*public static function checkPriceWithPrice($product, $productcmd, $rise_fall, $conditionsprice, $stop_profit_price = 0, $stop_loss_price = 0)
    {
        if (is_numeric($product)) {

            $product = SymbolXh::findOne($product);
        }
        if (empty($product)) {
            throw new \Exception('产品不存在');
        }
        if (!in_array(strtoupper($productcmd),
                ['NOW', 'GT', 'LT']) || !in_array(strtoupper($rise_fall), ['RISE', 'FALL'])) {
            throw new \Exception('买卖方向错误');
        }
        //获取最新价格
        Yii::$app->services->kline->prex = 'rf_addon_tf_futures_symbol';
        $newPrice                        = Yii::$app->services->kline->loadTicekt($product->symbol_id);
        if (empty($newPrice)) {
            throw new \Exception('最新价格获取失败');
        }
        $product_price = $newPrice->price;
        if ($productcmd == 'GTN' && $conditionsprice <= $product_price) {
            throw new \Exception('挂单价格不能低于' . $product_price);
        }else if ($productcmd == 'LTN' && $conditionsprice >= $product_price) {
            throw new \Exception('挂单价格不能高于' . $product_price);
        }
        $unit_price = bcdiv($product->unit_price, $product->unit, 4);
        if ($rise_fall == 'RISE') {
            $maxrise = $product->maxrise > 0
                ? bcadd($price, bcmul($product->maxrise, $unit_price, 4), 4)
                : 0;
            $minrise = $product->minrise > 0
                ? bcadd($price, bcmul($product->minrise, $unit_price, 4), 4)
                : 0;
            $maxlost = $product->maxlost > 0
                ? bcsub($price, bcmul($product->maxlost, $unit_price, 4), 4)
                : 0;
            $minlost = $product->minlost > 0
                ? bcsub($price, bcmul($product->minlost, $unit_price, 4), 4)
                : 0;
            if ($stop_profit_price > 0) {
                if ($maxrise > 0 && $stop_profit_price > $maxrise) {
                    throw new \Exception('止盈价格不能高于' . $maxrise);
                }
                if ($minrise > 0 && $stop_profit_price < $minrise) {
                    throw new \Exception('止盈价格不能低于' . $maxrise);
                }
            }
            if ($stop_loss_price > 0) {
                if ($maxlost > 0 && $stop_loss_price < $maxlost) {
                    throw new \Exception('止损价格不能低于' . $maxlost);
                }
                if ($minlost > 0 && $stop_loss_price > $minlost) {
                    throw new \Exception('止损价格不能高于' . $minlost);
                }
            }
        }elseif ($rise_fall == 'FALL') {
            $maxrise = $product->maxrise > 0
                ? bcsub($price, bcmul($product->maxrise, $unit_price, 4), 4)
                : 0;
            $minrise = $product->minrise > 0
                ? bcsub($price, bcmul($product->minrise, $unit_price, 4), 4)
                : 0;
            $maxlost = $product->maxlost > 0
                ? bcadd($price, bcmul($product->maxlost, $unit_price, 4), 4)
                : 0;
            $minlost = $product->minlost > 0
                ? bcadd($price, bcmul($product->minlost, $unit_price, 4), 4)
                : 0;
            if ($stop_profit_price > 0) {
                if ($maxrise > 0 && $stop_profit_price < $maxrise) {
                    throw new \Exception('止盈价格不能低于' . $maxrise);
                }
                if ($minrise > 0 && $stop_profit_price > $minrise) {
                    throw new \Exception('止盈价格不能高于' . $minrise);
                }
            }
            if ($stop_loss_price > 0) {
                if ($maxlost > 0 && $stop_profit_price > $maxlost) {
                    throw new \Exception('止盈价格不能低于' . $maxlost);
                }
                if ($minlost > 0 && $stop_profit_price < $minlost) {
                    throw new \Exception('止盈价格不能高于' . $minlost);
                }
            }
        }

        return ['code' => 200, 'msg' => 'ok'];
    }*/
    public function getMember_info()
    {
        return $this->hasOne(Member2::class, ['id' => 'member_id']);
    }
}
